/**********************************************************************************
***
***    Screencasting Teaching System C++ Library
***    Copyright (C) 2011  Jiri Novak <jiri.novak@petriny.net>
***                        Wadi Jalil Maluf <wadijm@gmail.com>
***
***    This file is part of the Screencasting Teaching System C++ Library.
***
***    This library is free software; you can redistribute it and/or modify
***    it under the terms of the GNU General Public License as published by
***   the Free Software Foundation; either version 3 of the License, or
***    (at your option) any later version.
***
***    This library is distributed in the hope that it will be useful,
***    but WITHOUT ANY WARRANTY; without even the implied warranty of
***    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
***   GNU General Public License for more details.
***
***    You should have received a copy of the GNU General Public License
***    along with this library; if not, write to the Free Software
***    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
***
*********************************************************************************/

#ifndef SCREENCASTING_TEACHING_SYSTEM___CLIENT__GUI___MAINWINDOW_H
#define SCREENCASTING_TEACHING_SYSTEM___CLIENT__GUI___MAINWINDOW_H

#include <gtkmm.h>
#include <list>
#include <log4cxx/logger.h>
#include "screencasting_window.h"

class CaptureWorkspace;
class LoginDialog;
class ScreencastingWindow;
class User;
class Course;
class Lesson;

/**
 *
 */
class MainWindow : public Gtk::Window {
public:
	MainWindow(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder);
	virtual ~MainWindow();

	// slots
	void onLoginSuccessfull(User *user);
	void onShowsListRequested(std::list<Course*> *courses,std::list<Lesson*> *lessons);
	void lessonNotAvailable(Lesson *l);
	void setCapturedLesson(Lesson *l) { capturedLesson_ = l; selectedLesson_ = l; }

protected:
	// slots
	void onQuitRequested();
	void onConnectRequested();
	void onDisconnectRequested();
	void onInstructorStartPauseButtonClicked();
	void onStudentJoinLessonCicked();
	void onItemInfoRequested(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column);
	bool onFilterRowVisible(const Gtk::TreeModel::const_iterator& iter);
	void onInstructorLessonChatButtonClicked();
	void onInstructorFinalizeScreencastButtonClicked();
	void onScreencastingWindowClosure();
	void onScreencastingWindowOpen();

	// tree model columns
	class ModelColumnsCoursesAndClases : public Gtk::TreeModel::ColumnRecord {
		public:
			ModelColumnsCoursesAndClases() { add(columnName_); add(columnLessonPointer_); add(columnCoursePointer_); }
			Gtk::TreeModelColumn<Glib::ustring> columnName_;
			Gtk::TreeModelColumn<Lesson*> columnLessonPointer_;
			Gtk::TreeModelColumn<Course*> columnCoursePointer_;
	};
	ModelColumnsCoursesAndClases treeModelCoursesAndClasesColumns_;
	Glib::RefPtr<Gtk::TreeStore> treeModelCoursesAndLessons_;
	Glib::RefPtr<Gtk::TreeModelFilter> treeModelCoursesAndLessonsFilter_;

private:
	log4cxx::LoggerPtr logger_;

	bool isSubscribed(const std::list<User*> &subscribedUsers) const;

	void initializeRelatedDialogs();
	void destroyRelatedDialogs();
	void initializeWidgets();
	void registerRelatedDialogs2ServerServiceHandler();
	void showErrorMessageDialog(const Glib::ustring &primary, const Glib::ustring &secondary);
	void makeConnections();
	void freeResources();

	void setLessonInfo();
	void setCourseInfo();

	// builder
	Glib::RefPtr<Gtk::Builder> builder_;

	// window consisting widgets
	Gtk::TreeView *treeViewCoursesAndLessons_;
	Gtk::TreeView *treeViewLessons_;
	Gtk::ImageMenuItem *menuItemQuit_;
	Gtk::ImageMenuItem *menuItemConnect_;
	Gtk::ImageMenuItem *menuItemDisconnect_;
	Gtk::ToolButton *toolButtonConnect_;
	Gtk::ToolButton *toolButtonDisconnect_;
	Gtk::Frame *frameCourse_;
	Gtk::Frame *frameLesson_;
	Gtk::Label *labelLessonName_;
	Gtk::Label *labelLessonDescription_;
	Gtk::Label *labelLessonDate_;
	Gtk::Label *labelCourseName_;
	Gtk::Label *labelCourseDescription_;
	Gtk::Label *labelCourseStartDate_;
	Gtk::Label *labelCourseEndDate_;
	Gtk::Label *labelScreencastingActive_;
	Gtk::Button *buttonInstructorStartPauseLesson_;
	Gtk::Button *buttonInstructorFinishLesson_;
	Gtk::Button *buttonInstructorLessonChat_;
	Gtk::Button *buttonStudentJoinLesson_;
	Gtk::CheckButton *checkButtonScreencastPersist_;

	// related dialogs
	LoginDialog *loginDialog_;
	ScreencastingWindow *screencastingWindow_;

	// session data
	User* user_;
	std::list<Course*> *courses_;
	std::list<Lesson*> *lessons_;
	Lesson *selectedLesson_;
	Course *selectedCourse_;

	// screencasting
	bool screencastingActive_;
	CaptureWorkspace *captureWorkspace_;
	Lesson *capturedLesson_;
};

#endif /* SCREENCASTING_TEACHING_SYSTEM___CLIENT__GUI___MAINWINDOW_H */
